/*------------------------------------------------------------------------------------------------\
| Horrible X-Wing game by Steven Haber
| st_haber@hotmail.com
|
| craft.h: contains class Craft, which is any ship (friend of foe) used in the game
\------------------------------------------------------------------------------------------------*/

#include "Craft.h"


//Constructor, takes the craft sprite, the initial position, and the render pointer
Craft::Craft(sf::Sprite craft, float initialX, float initialY, sf::RenderWindow *out, Sound *sounds)
{
	mCraft = craft;
	mCraft.SetRotationCenter(mCraft.GetWidth()/2, mCraft.GetHeight()/2);
	mOut = out;
	mVelX = (float)0;
	mVelY = (float)0;
	mSounds = sounds;

	mCraft.SetLeft(initialX);
	mCraft.SetTop(initialY);
}


//Used to change the X velocity of the craft
void Craft::alterCraftX(float velX)
{
	mVelX = velX;
}


//Y velocity
void Craft::alterCraftY(float velY)
{
	mVelY = velY;
}


//Moves and draws the craft based on the current velocities stored
void Craft::drawCraft()
{
	if(mVelX !=0 && mVelY != 0)
		mCraft.Move(mVelX/SQRT2, mVelY/SQRT2);	//If moving diagonal, divide by SQRT2
	else
		mCraft.Move(mVelX, mVelY);

	//These checks prevent it from moving outside of the window
	if(mCraft.GetLeft() < 0)
		mCraft.SetLeft(0);
	if(mCraft.GetLeft() > SCREENX - mCraft.GetWidth())
		mCraft.SetLeft(SCREENX - mCraft.GetWidth());
	if(mCraft.GetTop() < 0)
		mCraft.SetTop(0);
	if(mCraft.GetTop() > SCREENY - mCraft.GetHeight())
		mCraft.SetTop(SCREENY - mCraft.GetHeight());

	mOut -> Draw(mCraft);
}


//These next few are used for collisions...
float Craft::getTop()
{
	return mCraft.GetTop();
}
float Craft::getLeft()
{
	return mCraft.GetLeft();
}
float Craft::getWidth()
{
	return mCraft.GetWidth();
}
float Craft::getHeight()
{
	return mCraft.GetHeight();
}


//Makes it look derelict
void Craft::die()
{
	sf::Color deadColor(100, 100, 100, 255);
	mCraft.SetColor(deadColor);
	mCraft.SetRotation(sf::Randomizer::Random((float)0, (float)359));
	mSounds->playSound(DIE);
}